define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            $(".btn-dialog").data("area", ["100%", "100%"]); //添加
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kf/record/index' + location.search,
                    add_url: 'kf/record/add',
                    edit_url: 'kf/record/edit',
                    del_url: 'kf/record/del',
                    multi_url: 'kf/record/multi',
                    import_url: 'kf/record/import',
                    search_url: 'kf/record/search',
                    table: 'kf_record',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate: false},
                        {field: 'car_num', title: __('Car_num'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'phone', title: __('Phone'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'one_id', title: __('One_id')},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'edit',
                                    text: __('录入信息'), // 按钮文字，语言在对应的lang文件设置
                                    title: __('录入信息'), // 提示
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: '', // 更换图标
                                    url: 'kf/record/add',
                                    extend: 'data-area=["100%","100%"]',
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'kf/record/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '140px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'kf/record/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'kf/record/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        fuwudan: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'kf/record/fuwudan' + location.search,
                    table: 'list_one',
                }
            });
            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                // console.log(data);
                //这里我们手动设置底部的值
                $("#all_main_free").text(data.all_main_free);
                $("#all_main_sum").text(data.all_main_sum);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormTemplate: 'list_one',
                search: false,
                columns: [
                    [
                        // {checkbox: true},
                        // {field: 'id', title: __('Id')},
                        {
                            field: 'status',
                            title: __('状态'),
                            operate: false,
                            formatter: function (value, row, index) {
                                // console.log('index',row)
                                var html = '';
                                if (!row.tong_number) {
                                    html = "<p style=\"color:#32f800;font-weight: 700;border: 1px solid #32f800;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                        "                        暂存</p>";
                                } else {
                                    if (row.status == 6) {
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        退统</p>";
                                    } else if (row.status == 7) {
                                        html = "<p style=\"color:#fa0b0b;font-weight: 700;border: 1px solid #fa0b0b;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        作废</p>";
                                    } else {
                                        html = " <p style=\"color:#000;font-weight: 700;border: 1px solid #000;border-radius: 4px;width: 29px;height: 22px;line-height: 20px;text-align: center;display: inline-block;margin: 0;\">\n" +
                                            "                        正常</p>";
                                    }
                                }
                                return html;
                            }
                        },
                        {
                            field: 'tong_storage',
                            title: __('暂存单号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'tong_number',
                            title: __('服务单号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'zuzhi.name',
                            title: __('组织'),
                            searchList: $.getJSON('lists/one/select_zuzhi'),
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {field: 'yw_belonger', title: __('Yw_belonger'), operate: 'LIKE'},
                        {
                            field: 'car_num',
                            title: __('车牌号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_vin',
                            title: __('车架号'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'car_engine',
                            title: __('发动机号'),
                            visible: false,
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'learned_name',
                            title: __('被服务人'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_join_time',
                            title: __('服务日期'),
                            operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'yw_hetong_time',
                            title: __('初审日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'yw_shenhe_time',
                            title: __('审核日期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'plan_strattime',
                            title: __('服务起期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'plan_endtime',
                            title: __('服务止期'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false,
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'chexing',
                            title: __('车型'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'main_free',
                            title: __('应缴服务费'),
                            operate: false,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_enter_peo',
                            title: __('录入人'),
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'yw_hetongpeo',
                            title: __('审核人'),
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value) {
                                    return value;
                                } else {
                                    return '-';
                                }
                            }
                        },
                        {
                            field: 'sign',
                            title: __('签字状态'),
                            operate: false,
                            visible: Config.status,
                            table: table,
                            class: 'autocontent',
                            formatter: function (value, row, index) {
                                if (value == 1) {
                                    var html = "已签字";
                                    return html;
                                } else {
                                    var html = "未签字";
                                    return html;
                                }
                            }
                        },
                        {
                            field: 'status',
                            title: __('状态'),
                            searchList: {
                                0: __('暂存'),
                                1: __('待审核'),
                                2: __('待市级审核'),
                                3: __('审核通过待缴费'),
                                4: __('待修改'),
                                5: __('已出单'),
                                6: __('已退统'),
                                7: __('已过期'),
                            },
                            custom: {
                                0: 'success',
                                1: 'default',
                                2: 'warning',
                                3: 'info',
                                4: 'danger',
                                5: 'primary',
                                6: 'huang',
                                7: 'zi'
                            },
                            formatter: Table.api.formatter.label
                        },
                        {
                            field: 'car_factory_num',
                            title: __('厂牌型号'),
                            visible: false,
                            operate: Config.sxstatus,
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },

                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            // formatter: Table.api.formatter.operate,
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                // operate-edit编辑  perate-del删除
                                //判断什么时候显示什么时候不显示
                                // if (row.status === 1) {
                                //     $(table).data("operate-del", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                //     // $(table).data("operate-edit", null); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                // }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            },
                            buttons: [
                                {
                                    name: 'details',
                                    text: '浏览',
                                    classname: 'btn btn-info btn-xs btn-dialog',
                                    icon: '',
                                    url: 'buchang/baoan/onedetails',
                                    extend: 'data-area=["100%","100%"]',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },{
                                    name: 'addtabs',
                                    text: __('电子服务单'),
                                    title: __('电子服务单'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: '',
                                    url: 'lists/one/dian',
                                    visible: function (row) {
                                        //权限判断
                                        //返回true时按钮显示,返回false隐藏
                                        if (row.status == 5) {
                                            return true;
                                        }
                                    }
                                }
                            ]
                        }
                    ]
                ]
            });
            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        search: function () {
            Controller.api.bindevent();
        },
        tan: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
